package com.party.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.party.domain.Topic;

/**
 * 
 * @author Caizhf
 * @date 2017年5月12日下午9:33:50
 * @version v.0.1
 * @email 1115054416@qq.com 
 * 
 * <p>Description: 帖子mapper</p>
 * 级联有TopicMapper.xml文件
 */
public interface TopicMapper {
	
	//增加一个topic，注意：user不能为空！
	@Insert("INSERT INTO topic(topic_time,topic_content,topic_like,topic_title,user_id)"
			+ "VALUES(#{topic_time},#{topic_content},#{topic_like},#{topic_title},#{user.id})")
	@SelectKey(before=false,keyProperty="tid",resultType=Integer.class, statement = { "SELECT LAST_INSERT_ID()" })
	 int add(Topic topic);
	
	
	/**
	 * 根据用户的id查询这个用户的所有发帖，每个topic维护着user的引用,并且分页显示
	 * @param user_id 	用户id
	 * @param pageStart 从哪条记录开始
	 * @param pageSize	取多少条记录
	 * @return
	 */
	@Select("SELECT * FROM topic t,user u WHERE t.user_id=u.id AND u.id=#{user_id} "
			+ "LIMIT #{pageStart},#{pageSize}")
	@ResultMap("com.party.mapper.TopicMapper.TopicBaseMapped")
	 List<Topic> findUserAll(@Param("user_id")int user_id,@Param("pageStart")int pageStart,@Param("pageSize")int pageSize);
	
	//查询某个用户发帖的总数
	@Select("SELECT COUNT(*) FROM topic t,user u WHERE u.id=t.user_id AND u.id=#{user_id}")
	 int findUserTotleRecord(int user_id);
	
	
	@Select("SELECT COUNT(*) FROM topic")
	public int findTotleRecord();
	//查询所有的帖子，分页显示
	@Select("SELECT t.*,u.name,u.icon,u.id FROM topic t LEFT JOIN user u ON u.id=t.user_id"
			+ " LIMIT #{pageStart},#{pageSize}")
	@ResultMap("com.party.mapper.TopicMapper.TopicBaseMapped")
	 List<Topic> findAll(@Param("pageStart")int pageStart,@Param("pageSize")int pageSize);
	
	//根据id查找,每个topic维护着user的引用
	@Select("SELECT * FROM topic t,user u WHERE u.id=t.user_id AND tid=#{topic_id}")
	@ResultMap("com.party.mapper.TopicMapper.TopicBaseMapped")
	 Topic findById(int topic_id);
	
	
	//根据id删除
	@Delete("DELETE FROM topic WHERE tid=#{topic_id}")
	 int deleteById(int topic_id);
	
	
	//修改已经发布过的帖子
	@Update("UPDATE topic SET "
			+ "topic_time=#{topic_time},topic_content=#{topic_content},topic_title=#{topic_title}"
			+ " WHERE tid=#{tid}")
	 int update(Topic topic);
	
	//点赞增加
	@Update("UPDATE topic SET topic_like=#{user_ids} WHERE tid=#{topic_id}")
	 int updatelikeById(@Param("user_ids")String user_ids,@Param("topic_id") int topic_id);
	
}
